// 引入 express    npm install express
let express = require('express');
let router = express.Router();
let query = require('./Database');
// 引入 fs
let fs = require('fs');
// 引入 body-parser     npm install body-parser
let bodyParser = require("body-parser");
// 引模块:npm install mysql
const mysql = require('mysql');
//
var path = require('path');

//处理前端页面返回
router.get('/', function(req, res, next) {
    res.render('MyshoppingCart', {});
});
//返回页面
router.post('/ShoppingCart',async (req, res) => {
    let id = req.body.userid;
    let Shop = await  query('select distinct goods.shopid,shop.shopname from goods join shop where  userid='+ id +' and goods.shopid = shop.id and goods.state =11');
    console.log('+++++++++++++');
    let Product= await query('select * from goods where goods.state =11 and userid='+ id +' order by userid');
    console.log('Product');
    let data = [{Shop:Shop},{Product:Product},{code:100}];
    res.send(data);
});
//删除
router.post('/Delete',async (req, res) => {
    let Del = req.body;
    let Shop = await  query('update goods set state = ' + Del.state + '  where id=' + Del.id);
});
//提交订单
router.post('/OrderCart',async (req, res) => {
    let userid = req.body.OrderIdArr;
    let useridArr = userid.split(',');
    for(let i=0;i<useridArr.length;i++){
        await query('update goods set goods.state = 2 where id=' + useridArr[i] + '');
    }
    let data = {
        code:100
    };
    res.send(data);
});
module.exports = router;